package com.my.simple.web.controller.admin;

import com.my.simple.common.util.MapStructUtil;
import com.my.simple.entity.auth.User;
import com.my.simple.entity.auth.UserRole;
import com.my.simple.service.auth.UserService;
import com.my.simple.web.vo.ResponseData;
import com.my.simple.web.vo.UserVO;
import com.my.simple.web.vo.mapstruct.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "user")
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    private static final Integer USERID=10000;
    @Autowired
    private UserService userService;

    @PostMapping(value = "adduser")
    public ResponseEntity<ResponseData> addUser(@RequestBody User userItem) {
        ResponseData responseData = new ResponseData();
        userService.addUser(userItem);
        LOGGER.info("add user id is {}", userItem.getUserId());

        responseData.setMessage("add successful");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @GetMapping(value = "getalluser")
    public ResponseEntity<ResponseData> getAllUser() {
        ResponseData responseData = new ResponseData();
        List<User> userList = userService.getAllUser();
        responseData.setData(userList);
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @GetMapping(value = "getuserbyid/{userid}")
    public ResponseEntity<ResponseData> getUserById(@PathVariable("userid") int userId) {
        ResponseData responseData = new ResponseData();
        User user = userService.getUserById(userId);
        UserMapper mapper = MapStructUtil.getInstance(UserMapper.class);
        UserVO userVO = mapper.map2UserVO(user);

        responseData.setData(userVO);
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @DeleteMapping(value = "deleteuserbyid/{userid}")
    public ResponseEntity<ResponseData> deleteUserById(@PathVariable("userid") int userId) {
        ResponseData responseData = new ResponseData();
        userService.deleteUserById(userId);
        responseData.setMessage("delete successful");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @PutMapping(value = "updateuser")
    public ResponseEntity<ResponseData> updateUser(@RequestBody User userItem) {
        ResponseData responseData = new ResponseData();
        Integer userId = userItem.getUserId();
        User findUser = userService.getUserById(userId);
        findUser.setUserName(userItem.getUserName());
        findUser.setEmail(userItem.getEmail());
        findUser.setBirthday(userItem.getBirthday());
        findUser.setPassword(userItem.getPassword());
        findUser.setSalt(userItem.getSalt());
        findUser.setUserAccount(userItem.getUserAccount());
        findUser.setAddress(userItem.getAddress());
        findUser.setMobile(userItem.getMobile());
        findUser.setActive(userItem.getActive());
        findUser.setUpdateDate(new Date());
        findUser.setUpdateBy(USERID);
        
        userService.updateUser(findUser);
        responseData.setMessage("update user success");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @PutMapping(value = "updateuserrole")
    public ResponseEntity<ResponseData> updateUserRole(@RequestBody List<UserRole> userRoleEntityList) {
        ResponseData responseData = new ResponseData();
        userService.updateUserRole(userRoleEntityList);
        responseData.setMessage("add successful");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @GetMapping(value = "getuserrolebyuserid/{userid}")
    public ResponseEntity<ResponseData> getUserRoleByUserId(@PathVariable("userid") int userId) {
        ResponseData responseData = new ResponseData();
        List<UserRole> userRoleEntityList = userService.getUserRoleByUserId(userId);
        responseData.setData(userRoleEntityList);
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }
}
